import * as Koa from "koa"
import log4js from 'log4js'
const logger  = async (ctx: Koa.Context, next: Function) => {
    const loggers = log4js.getLogger()
    const start: number = new Date().getTime()
    loggers.debug('------------------------------------------------------')
    loggers.info(`url------${JSON.stringify(ctx.method).toLocaleUpperCase()}-------${ctx.url}`)
    loggers.info(`header------${JSON.stringify(ctx.request.header)}`)
    loggers.info(`query------${JSON.stringify(ctx.request.query)}`)
    loggers.info(`body------${JSON.stringify(ctx.request.body)}`)
    const body = await next()
    if (body) {
        ctx.body = body
    } else {
        if (!ctx.body) {
            ctx.body = {}
        }
    }
    const ms: number = new Date().getTime() - start
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
    loggers.info(`${ctx.method} ${ctx.url} ----${ctx.status}---- ${JSON.stringify(ctx.body)}`)
}
export default logger
